package com.autumn.platform.core.xml.context.impl;

import org.springframework.beans.factory.parsing.FailFastProblemReporter;
import org.springframework.beans.factory.parsing.NullSourceExtractor;
import org.springframework.beans.factory.parsing.ProblemReporter;
import org.springframework.beans.factory.parsing.SourceExtractor;
import org.springframework.beans.factory.xml.DefaultDocumentLoader;
import org.springframework.beans.factory.xml.DocumentLoader;
import org.springframework.core.env.Environment;
import org.xml.sax.EntityResolver;
import org.xml.sax.ErrorHandler;

import com.autumn.platform.core.spring.SpringHolder;
import com.autumn.platform.core.xml.BaseErrorHandler;
import com.autumn.platform.core.xml.PropertiesEntityResolver;
import com.autumn.platform.core.xml.context.IXmlParserContext;
import com.autumn.platform.core.xml.context.XmlValidationMode;

public class DefaultXmlParserContext implements IXmlParserContext {

    private ProblemReporter problemReporter;

    private SourceExtractor sourceExtractor;

    private Environment environment;

    private DocumentLoader documentLoader;

    private EntityResolver entityResolver;

    private ErrorHandler errorHandler;

    private XmlValidationMode xmlValidationMode;

    @Override
    public ProblemReporter getProblemReporter() {
        if (null == this.problemReporter) {
            this.problemReporter = new FailFastProblemReporter();
        }
        return problemReporter;
    }

    public void setProblemReporter(ProblemReporter problemReporter) {
        this.problemReporter = problemReporter;
    }

    @Override
    public SourceExtractor getSourceExtractor() {
        if (null == this.sourceExtractor) {
            this.sourceExtractor = new NullSourceExtractor();
        }
        return sourceExtractor;
    }

    public void setSourceExtractor(SourceExtractor sourceExtractor) {
        this.sourceExtractor = sourceExtractor;
    }

    @Override
    public Environment getEnvironment() {
        if (null == this.environment) {
            this.environment = SpringHolder.getEnvironment();
        }
        return environment;
    }

    public void setEnvironment(Environment environment) {
        this.environment = environment;
    }

    @Override
    public DocumentLoader getDocumentLoader() {
        if (null == this.documentLoader) {
            this.documentLoader = new DefaultDocumentLoader();
        }
        return this.documentLoader;
    }

    public void setDocumentLoader(DocumentLoader documentLoader) {
        this.documentLoader = documentLoader;
    }

    @Override
    public EntityResolver getEntityResolver() {
        if (null == this.entityResolver) {
            PropertiesEntityResolver per = new PropertiesEntityResolver();
            try {
                per.afterPropertiesSet();
            } catch (Exception e) {
                throw new IllegalStateException(e);
            }
            this.entityResolver = per;
        }
        return this.entityResolver;
    }

    public void setEntityResolver(EntityResolver entityResolver) {
        this.entityResolver = entityResolver;
    }

    @Override
    public ErrorHandler getErrorHandler() {
        if (null == this.errorHandler) {
            this.errorHandler = new BaseErrorHandler();
        }
        return this.errorHandler;
    }

    public void setErrorHandler(ErrorHandler errorHandler) {
        this.errorHandler = errorHandler;
    }

    @Override
    public XmlValidationMode getXmlValidationMode() {
        if (null == this.xmlValidationMode) {
            this.xmlValidationMode = XmlValidationMode.XSD;
        }
        return xmlValidationMode;
    }

    public void setXmlValidationMode(XmlValidationMode xmlValidationMode) {
        this.xmlValidationMode = xmlValidationMode;
    }
}
